package com.siss.tenancy.util;

import com.siss.tenancy.vo.EleTreeBasicVo;
import com.siss.tenancy.vo.EleTreeVo;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>类名: EleTreeUtil</p>
 * <p>描述:layui-eletree工具类，用于生成相应的树状模型</p>
 * <p>创建人: wangxj</p>
 * <p>创建时间: 2018/10/16 14:05</p>
 * <p>@version 2.0  </p>
 * <p>修改内容: ......</p>
 * <p>修改说明: ......</p>
 * <p>修改时间: ......</p>
 * <p>修改人: ......</p>
 * <p>每次修改，请增加上述修改信息说明</>
 */
public class EleTreeUtil {

    /**
     * 功能描述：根据指定集合生成layui-eletree
     * 作   者： wangxj
     * 创建时间： 2018/10/16 16:30
     * @param rootNodeOrgTypeId
     * @param eleTreeBasicVos
     * @param getNodeLevel
     * @param spreadNodeLevel
     *  返回值： java.util.List<com.szyf.siss.biz.common.vo.EleTreeVo>
     */
    public static List<EleTreeVo> selectEleTreeVo(String rootNodeId, List<EleTreeBasicVo> eleTreeBasicVos, Integer currentNodeLevel, Integer getNodeLevel, Integer spreadNodeLevel){
        //获取父组织类型id集合
        List<String> parentOrgTypeIds = getParentOrgTypeIds(eleTreeBasicVos);
        //获取子组织类型id集合
        List<String> childOrgTypeIds = getChildOrgTypeIds(eleTreeBasicVos);
        //获取根节点组织类型集合
        List<String> rootOrgTypeIds=new ArrayList<String>();
        if(StringUtil.isEmpty(rootNodeId)){
            rootOrgTypeIds=parentOrgTypeIds;
            rootOrgTypeIds.removeAll(childOrgTypeIds);
        }
        if(StringUtil.isNotEmpty(rootNodeId)){
            rootOrgTypeIds.add(rootNodeId);
        }
        List<EleTreeVo> rootNodes=new ArrayList<>();;
        for (String rootOrgTypeId : rootOrgTypeIds) {
            EleTreeVo rootNode=new EleTreeVo();
            for (EleTreeBasicVo eleTreeeBasicVo : eleTreeBasicVos) {
                if(rootOrgTypeId.equals(eleTreeeBasicVo.getParentId())){
                    rootNode.setId(eleTreeeBasicVo.getParentId());
                    rootNode.setLabel(eleTreeeBasicVo.getParentName());
                    rootNode.setChecked(eleTreeeBasicVo.getIsChecked());
                    rootNode.setDisabled(eleTreeeBasicVo.getIsDisabled());
                    rootNode.setSpread(true);
                    currentNodeLevel=null==currentNodeLevel?1:currentNodeLevel;
                    getNodeLevel=StringUtil.isNotEmpty(rootNodeId)&&100==getNodeLevel?currentNodeLevel+1:getNodeLevel+currentNodeLevel;
                    spreadNodeLevel=StringUtil.isNotEmpty(rootNodeId)&&3==spreadNodeLevel?currentNodeLevel+1:spreadNodeLevel+currentNodeLevel;
                    rootNode.setNodeLevel(currentNodeLevel);
                    rootNode=selectEleTreeNodes(eleTreeBasicVos,rootNode,currentNodeLevel,getNodeLevel,spreadNodeLevel);
                    rootNodes.add(rootNode);
                    break;
                }
            }
        }
        
        if (rootNodes.size() < 1) {
        	for (String rootOrgTypeId : rootOrgTypeIds) {
                EleTreeVo rootNode=new EleTreeVo();
                for (EleTreeBasicVo eleTreeeBasicVo : eleTreeBasicVos) {
                    if(rootOrgTypeId.equals(eleTreeeBasicVo.getChildId())){
                        rootNode.setId(eleTreeeBasicVo.getChildId());
                        rootNode.setLabel(eleTreeeBasicVo.getChildName());
                        rootNode.setChecked(eleTreeeBasicVo.getIsChecked());
                        rootNode.setDisabled(eleTreeeBasicVo.getIsDisabled());
                        rootNode.setSpread(true);
                        currentNodeLevel=null==currentNodeLevel?1:currentNodeLevel;
                        getNodeLevel=StringUtil.isNotEmpty(rootNodeId)&&100==getNodeLevel?currentNodeLevel+1:getNodeLevel+currentNodeLevel;
                        spreadNodeLevel=StringUtil.isNotEmpty(rootNodeId)&&3==spreadNodeLevel?currentNodeLevel+1:spreadNodeLevel+currentNodeLevel;
                        rootNode.setNodeLevel(currentNodeLevel);
                        rootNode=selectEleTreeNodes(eleTreeBasicVos,rootNode,currentNodeLevel,getNodeLevel,spreadNodeLevel);
                        rootNodes.add(rootNode);
                        break;
                    }
                }
            }
		}
        return rootNodes;
    }

    /**
     * 功能描述：递归树形
     * 作   者： wangxj
     * 创建时间： 2018/10/16 14:24
     * @param eleTreeBasicVos
     * @param eleTreeVo
     * @param currentNodeLevel
     *  返回值： com.szyf.siss.biz.common.vo.EleTreeVo
     */
    private static EleTreeVo selectEleTreeNodes(List<EleTreeBasicVo> eleTreeBasicVos,EleTreeVo eleTreeVo,int currentNodeLevel,Integer getNodeLevel,Integer spreadNodeLevel){
        List<EleTreeVo> childNodes=new ArrayList<>();
        currentNodeLevel++;
        Boolean spead=false;
        if(currentNodeLevel<=spreadNodeLevel){
            spead=true;
        }
        for (EleTreeBasicVo eleTreeBasicVo : eleTreeBasicVos) {
            if(eleTreeVo.getId().equals(eleTreeBasicVo.getParentId())){
                EleTreeVo childNode=new EleTreeVo();
                childNode.setId(eleTreeBasicVo.getChildId());
                childNode.setLabel(eleTreeBasicVo.getChildName());
                childNode.setNodeLevel(currentNodeLevel);
                childNode.setChecked(eleTreeBasicVo.getIsChecked());
                childNode.setDisabled(eleTreeBasicVo.getIsDisabled());
                childNode.setSpread(spead);
                childNodes.add(childNode);
            }
        }
        if(childNodes.size()>0){
            eleTreeVo.setChildrenSize(childNodes.size());
            if(currentNodeLevel>getNodeLevel){
                return eleTreeVo;
            }
            eleTreeVo.setChildren(childNodes);
            for (EleTreeVo childNode : childNodes) {
                selectEleTreeNodes(eleTreeBasicVos,childNode,currentNodeLevel,getNodeLevel,spreadNodeLevel);
            }
        }
        return eleTreeVo;
    }
    /**
     * 功能描述：根据基础数据实体获取子组织类型id集合
     * 作   者： wangxj
     * 创建时间： 2018/10/16 14:24
     * @param eleTreeBasicVos
     *  返回值： java.util.List<java.lang.String>
     */
    private static List<String> getChildOrgTypeIds(List<EleTreeBasicVo> eleTreeBasicVos) {
        List<String> childOrgTypeIds=new ArrayList<String>();
        for (EleTreeBasicVo eleTreeBasicVo : eleTreeBasicVos) {
            childOrgTypeIds.add(eleTreeBasicVo.getChildId());
        }
        return childOrgTypeIds;
    }

    /**
     * 功能描述：根据基础数据实体获取父组织类型集合
     * 作   者： wangxj
     * 创建时间： 2018/10/16 14:24
     * @param eleTreeBasicVos
     *  返回值： java.util.List<java.lang.String>
     */
    private static List<String> getParentOrgTypeIds(List<EleTreeBasicVo> eleTreeBasicVos) {
        List<String> parentOrgTypeIds=new ArrayList<String>();
        for (EleTreeBasicVo eleTreeBasicVo : eleTreeBasicVos) {
            if(!parentOrgTypeIds.contains(eleTreeBasicVo.getParentId())){
                parentOrgTypeIds.add(eleTreeBasicVo.getParentId());
            }
        }
        return parentOrgTypeIds;
    }

    /**
     * 功能描述：根据树形获取指定节点所有子节点id集合
     * 作   者： wangxj
     * 创建时间： 2018/10/17 18:52
     * @param eleTreeVos 树形
     * @param parentNodeId 父节点id
     *  返回值： java.util.List<java.lang.String>
     */
    public static List<String> getChildNodeIdsByTrees(List<EleTreeVo> eleTreeVos,String parentNodeId){
        List<String> childNodeIds=null;
        for (EleTreeVo eleTreeVo : eleTreeVos) {
            if(eleTreeVo.getId().equals(parentNodeId)){
                childNodeIds=new ArrayList<>();
                childNodeIds=getChildNodeIdsByTree(eleTreeVo,childNodeIds);
            }
        }
        return childNodeIds;
    }

    /**
     * 功能描述：获了单节点所有子节点id集合
     * 作   者： wangxj
     * 创建时间： 2018/10/17 19:04
     * @param eleTreeVo
     * @param childNoIds
     *  返回值： java.util.List<java.lang.String>
     */
    private static List<String> getChildNodeIdsByTree(EleTreeVo eleTreeVo,List<String> childNoIds){
        if(null!=eleTreeVo){
            List<EleTreeVo> eleTreeVos=eleTreeVo.getChildren();
            if(null!=eleTreeVos){
                if(eleTreeVos.size()>0){
                    for (EleTreeVo treeVo : eleTreeVos) {
                        childNoIds.add(treeVo.getId());
                        if(treeVo.getChildren().size()>0){
                            getChildNodeIdsByTree(treeVo,childNoIds);
                        }
                    }
                }
            }
        }
        return childNoIds;
    }
}
